using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using CodePlex.TfsLibrary.Utility;

namespace CodePlex.CodePlexClientLibrary
{
    public static class CodePlexClientUtil
    {
        public static string GetFullPath(string pathName)
        {
            try
            {
                return Path.GetFullPath(pathName).TrimEnd('\\');
            }
            catch (ArgumentException ex)
            {
                throw new CommandLineException(ex.Message);
            }
            catch (NotSupportedException ex)
            {
                throw new CommandLineException(ex.Message);
            }
        }

        public static string GetRelativePath(string basePath,
                                             string path)
        {
            string relativePath = FileUtil.GetRelativePath(basePath, path);
            return relativePath == "" ? "." : relativePath;
        }

        public static void ParseProjectName(string projectText,
                                            out string projectName,
                                            out string projectPath)
        {
            string[] parts = projectText.Split(new char[] { ':' }, 2);
            projectName = parts[0];
            projectPath = "/";

            if (parts.Length == 2)
                projectPath = parts[1];
        }

        public static Process StartProcess(string command,
                                           string arguments)
        {
            try
            {
                return Process.Start(command, arguments);
            }
            catch (Win32Exception)
            {
                throw new CommandLineException("Could not launch external program: {0}", command);
            }
        }
    }
}